﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RestaurateurConsole
{
    public partial class ConfigOptions : Form
    {
        public ConfigOptions()
        {
            InitializeComponent();
        }

        private void ConfigOptions_Load(object sender, EventArgs e)
        {
            textBox1.Text = ConfigHandler.Configuration.TaxRate.ToString();
            textBox2.Text = ConfigHandler.Configuration.PhoneNumber;
            foreach (var line in ConfigHandler.Configuration.Address)
            {
                textBox3.Text += line + "\r\n";
            }
            textBox4.Text = ConfigHandler.Configuration.NoOfTables.ToString();
            textBox5.Text = ConfigHandler.Configuration.TaxAdditionThreshhold.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                decimal TaxRate = Decimal.Parse(textBox1.Text);
                if (TaxRate < (decimal)0)
                    throw new Exception("Tax rate cannot be negative.");
                ConfigHandler.Configuration.TaxRate = TaxRate;


                ConfigHandler.Configuration.PhoneNumber = textBox2.Text;
                ConfigHandler.Configuration.Address = textBox3.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                int NoOfTables = Int32.Parse(textBox4.Text);
                if (NoOfTables < 1)
                    throw new Exception("There has to be at least one table.");
                ConfigHandler.Configuration.NoOfTables = NoOfTables;

                decimal TaxAddnThreshhold = Decimal.Parse(textBox5.Text);
                if (TaxAddnThreshhold < (decimal)0)
                    throw new Exception("Tax addition threshold cannot be negative.");
                ConfigHandler.Configuration.TaxAdditionThreshhold = TaxAddnThreshhold;

                ConfigHandler.Configuration.Save();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
                return;
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                textBox1.Text = (0).ToString(); ;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
                textBox5.Text = (0).ToString();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
                textBox4.Text = (24).ToString();
        }
    }
}
